package com.tim.app.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.application.library.runtime.event.EventListener;
import com.application.library.runtime.event.EventManager;
import com.application.library.widget.smarttab.SmartTabLayout;
import com.tim.app.R;
import com.tim.app.constant.EventTag;
import com.tim.app.server.logic.UserManager;
import com.tim.app.ui.adapter.TabAdapter;
import com.tim.app.ui.fragment.friend.ConcernedFragment;
import com.tim.app.ui.fragment.friend.FansFragment;
import com.tim.app.ui.fragment.friend.FriendFragment;

/**
 * 用户列表
 */
public class UserFriendActivity extends BaseActivity {

    private static final String TAG = "UserFriendActivity";

    private ImageView ivBack;
    private TextView tvTitle;
    private ImageView ivAddFriend;

    private SmartTabLayout tab_layout;
    private ViewPager vpFriend;
    private TabAdapter pageAdapter;

    FriendFragment friendFragment;
    ConcernedFragment concernedFragment;
    FansFragment fansFragment;

    private int currentIndex;

    public static void start(Context context) {
        Intent intent = new Intent(context, UserFriendActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onBeforeSetContentLayout() {
        super.onBeforeSetContentLayout();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_user_friend;
    }

    @Override
    public void initView() {
        tvTitle = (TextView) findViewById(R.id.tvTitle);
        ivBack = (ImageView) findViewById(R.id.ivBack);
        ivAddFriend = (ImageView) findViewById(R.id.ivAddFriend);
        ivBack.setOnClickListener(this);
        ivAddFriend.setOnClickListener(this);

        tab_layout = (SmartTabLayout) findViewById(R.id.tab_layout);
        vpFriend = (ViewPager) findViewById(R.id.vpFriend);
        pageAdapter = new TabAdapter(getSupportFragmentManager());
        friendFragment = new FriendFragment();
        concernedFragment = ConcernedFragment.newInstance(UserManager.ins().getLoginUser());
        fansFragment = FansFragment.newInstance(UserManager.ins().getLoginUser());
        pageAdapter.addFragment(friendFragment, "好友");
        pageAdapter.addFragment(concernedFragment, getString(R.string.follow));
        pageAdapter.addFragment(fansFragment, getString(R.string.fans));
        vpFriend.setOffscreenPageLimit(2);
        vpFriend.setAdapter(pageAdapter);
        vpFriend.setCurrentItem(0);
        tab_layout.setViewPager(vpFriend);
        EventManager.ins().registListener(EventTag.ON_FRIEND_LOAD_COMPLETED, eventListener);
        EventManager.ins().registListener(EventTag.ON_FOLLOW_TITLT_LOAD_COMPLETED, eventListener);
        EventManager.ins().registListener(EventTag.ON_FANS_TITLT_LOAD_COMPLETED, eventListener);
        vpFriend.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                currentIndex = position;
                if (position == 0) {
                    if (friendFragment.getFriendCount() > 0) {
                        tvTitle.setText(getString(R.string.friendCount, String.valueOf(friendFragment.getFriendCount())));
                    } else {
                        tvTitle.setText("好友");
                    }
                } else if (position == 1) {
                    if (concernedFragment.getFollowCount() > 0) {
                        tvTitle.setText(getString(R.string.followCount, String.valueOf(concernedFragment.getFollowCount())));
                    } else {
                        tvTitle.setText("关注");
                    }
                } else if (position == 2) {
                    if (fansFragment.getFansCount() > 0) {
                        tvTitle.setText(getString(R.string.fansCount, String.valueOf(fansFragment.getFansCount())));
                    } else {
                        tvTitle.setText("粉丝");
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    EventListener eventListener = new EventListener() {
        @Override
        public void handleMessage(int what, int arg1, int arg2, Object dataobj) {
            switch (what) {
                case EventTag.ON_FRIEND_LOAD_COMPLETED:
                    int count = (int) dataobj;
                    if (currentIndex == 0) {
                        if (count > 0) {
                            tvTitle.setText(getString(R.string.friendCount, String.valueOf(count)));
                        } else {
                            tvTitle.setText("好友");
                        }
                    }
                    break;
                case EventTag.ON_FOLLOW_TITLT_LOAD_COMPLETED:
                    int followCount = (int) dataobj;
                    if (currentIndex == 1) {
                        if (followCount > 0) {
                            tvTitle.setText(getString(R.string.followCount, String.valueOf(followCount)));
                        } else {
                            tvTitle.setText("关注");
                        }
                    }
                    break;
                case EventTag.ON_FANS_TITLT_LOAD_COMPLETED:
                    int fanscount = (int) dataobj;
                    if (currentIndex == 2) {
                        if (fanscount > 0) {
                            tvTitle.setText(getString(R.string.fansCount, String.valueOf(fanscount)));
                        } else {
                            tvTitle.setText("粉丝");
                        }
                    }
                    break;
            }
        }
    };

    @Override
    public void initData() {

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.ivBack:
                finish();
                break;
            case R.id.ivAddFriend:
                break;
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventManager.ins().removeListener(EventTag.ON_FRIEND_LOAD_COMPLETED, eventListener);
        EventManager.ins().removeListener(EventTag.ON_FOLLOW_TITLT_LOAD_COMPLETED, eventListener);
        EventManager.ins().removeListener(EventTag.ON_FANS_TITLT_LOAD_COMPLETED, eventListener);
    }

}
